package etnorservice.etnor_service.service.impl;

import etnorservice.etnor_service.domain.po.User;
import etnorservice.etnor_service.domain.po.WorkspaceTreeEntity;
import etnorservice.etnor_service.mapper.WorkspaceTreeMapper;
import etnorservice.etnor_service.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/08/30/11:24
 **/
@Service
public class WorkspceService {
    @Autowired
    private WorkspaceTreeMapper workspaceTreeMapper;
    @Autowired
    private UserService userService;

    public WorkspaceTreeEntity getTree(String rootId) {
        User roleByAccount = userService.findRoleByAccount(Integer.parseInt(rootId));
        WorkspaceTreeEntity root = workspaceTreeMapper.findTreeById(rootId);
        loadChildren(root,rootId);
        return root;
    }

    private void loadChildren( WorkspaceTreeEntity node, String adminId) {

        node.setProblemDOList(workspaceTreeMapper.
                findProblemById(
                        node.getId(),adminId));
        if (node.getIsparent() == 1) {
            List<WorkspaceTreeEntity> children = workspaceTreeMapper.findChildrenNode(node.getId());
            node.setChildren(children);
            for (WorkspaceTreeEntity child : children) {
                loadChildren(child,adminId);  // 递归加载子节点
            }
        }
    }
    public List<WorkspaceTreeEntity> getTopList(){
        return workspaceTreeMapper.findTreeParent();
    }
}
